【LeetCode 26】Remove Duplicates from Sorted Array 删除排序数组中的重复项


“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
* 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
*/

public class leetcode26 {
public int removeDuplicates (int[] nums) {
int pos = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[pos] != nums[i]) {
pos++;
nums[pos] = nums[i];
}
}
return pos+1;
}
}
Thanks!